import 'package:valovault_reminder/common/config/val_assets_name.dart';
import 'package:valovault_reminder/pages/base/val_app_common_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

typedef VoidBlock = void Function();

abstract class ValBaseUIPageState<T extends StatefulWidget> extends State<T> with AutomaticKeepAliveClientMixin {
  bool isStatusBar = true; // 是否进行状态栏修改
  Brightness statusBarStyle = Brightness.dark; //状态栏字体颜色
  Color statusBarColor = Colors.transparent; // 状态栏颜色
  bool statusBarTranslucent = true; // 是否浸入式
  bool isAppBar = true; // 是否显示appbar
  bool resizeToAvoidBottomInset = true;
  Color bgColor = Colors.white; // 背景颜色
  // AppBar appBar; // 自定义appbar
  PreferredSizeWidget? appBar;
  VoidBlock? keepAliveBlock;
  bool extendBody = true;
  bool extendBodyBehindAppBar = false; //顶部从导航栏开始
  bool isDoubleClick = false; // 是否双击返回桌面
  DateTime? _clickTime; // 返回键点击时间
  FloatingActionButtonLocation? floatingLocation;

  @override
  bool get wantKeepAlive => false;

  @protected
  Widget buildBody(BuildContext context);

  Widget? buildFloating(BuildContext context) {
    return null;
  }

  Widget? buildNavigationBar(BuildContext context) {
    return null;
  }

  //appbar 标题
  String appBarTitle(BuildContext context) {
    return "";
  }

  ValAppCommonBar lightAppBar({String title = ''}) {
    return ValAppCommonBar(
      title: title,
      backgroundColor: Colors.transparent,
      leadingWidget: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: Image.asset(
            '${ValAssetsName.ASSETS_IMAGES}val_my_home_back_btn.png',
            width: 30,
            height: 20,
          )
      ),
    );
  }

  ValAppCommonBar darkAppBar({String title = ''}) {
    return ValAppCommonBar(
      title: title,
      brightness: Brightness.dark,
      titleColor: Colors.white,
      backgroundColor: Colors.transparent,
      leadingWidget: Container(),
    );
  }

  Widget _buildBase(BuildContext context) {
    return Scaffold(
      appBar: isAppBar ? (appBar ?? AppBar(title: Text(appBarTitle(context), style: const TextStyle(color: Colors.black),), centerTitle: true,
        systemOverlayStyle: SystemUiOverlayStyle(statusBarBrightness: statusBarStyle),
        backgroundColor: Colors.white,
      )) : null,
      body: buildBody(context),
      extendBodyBehindAppBar: extendBodyBehindAppBar,
      extendBody: extendBody,
      bottomNavigationBar: buildNavigationBar(context),
      backgroundColor: bgColor,
      floatingActionButton: buildFloating(context),
      floatingActionButtonLocation: floatingLocation,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    if (this.keepAliveBlock != null) {
      this.keepAliveBlock!();
    }
    return isDoubleClick
        ? WillPopScope(child: _buildBase(context), onWillPop: _onWillPop,)
        : _buildBase(context);
  }

  /// 点击返回键的操作
  Future<bool> _onWillPop() {
    if (_clickTime != null &&
        DateTime.now().difference(_clickTime!) < Duration(seconds: 2)) {
      return Future.value(true);
    } else {
      _clickTime = DateTime.now();
      // ToastUtil.show("再按一次退出应用");
      return Future.value(false);
    }
  }
}
